$(function () {
    $("#jqGrid").jqGrid({
        url: apiURL + 'sys/users/findListByCond',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'userId', index: "user_id", width: 45, key: true },
			{ label: '用户名', name: 'username', index: "username", width: 75 },
			{ label: '注册时间', name: 'createTime', index: "create_time", width: 90 ,formatter: function(value, options, row){
				return value ? value.split(" ")[0] : "" ;
			 }},
			/*{ label: '最后登录时间', name: 'lastLoginTime', width: 100 ,formatter: function(value, options, row){
				return value ? value.split(" ")[0] : "" ;
			 }},
			{ label: '位置信息', name: 'location', width: 80},*/
			{ label: '账号状态', name: 'status', index:"status", width: 80, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},
			{ label: '操作', name: 'userId', index: 'user_id', width: 120 ,formatter: function(value, options, row){
				var html = "<a class='btn btn-primary' onclick='javascript:vm.look("+value+")' >查看详情</a>&nbsp;&nbsp;&nbsp;&nbsp;";
				
				return html;
		 }}
        ],
		viewrecords: true,
        height: Commons.getGridHeight("jqGrid"),
        rowNum: 30,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		user:{
			username: "",
			startDate:"",
			endDate:"",
			mobile:""
		},
		showList: true,
		title:null,
		userm:{}
	},
	methods: {
		query: function () {
			//保存查询条件
            vm.userm = new Object();
			vm.userm.username = vm.user.username;
			vm.userm.startDate = vm.user.startDate;
			vm.userm.endDate = vm.user.endDate;
			vm.userm.mobile = vm.user.mobile;
			
			vm.reload();
		},
		look: function (cid) {
			vm.showList = false;
            vm.title = "查看详情";
            
            //保存查询条件
            vm.userm = new Object();
			vm.userm.username = vm.user.username;
			vm.userm.startDate = vm.user.startDate;
			vm.userm.endDate = vm.user.endDate;
			vm.userm.mobile = vm.user.mobile;
            
            vm.getUser(cid);
		},
		getUser: function(userId){
			$.get(apiURL + "sys/users/findUserByUserId/"+userId, function(r){
				vm.user = r.user;
			});
		},	
		reload: function () {
			//还原查询条件
			vm.user = vm.userm;
			
			vm.showList = true;
			
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'username': vm.user.username,'startDate': vm.user.startDate,'endDate': vm.user.endDate,'mobile': vm.user.mobile},
                page:page
            }).trigger("reloadGrid");
		},
		openOrClose:function(status){
			confirm("您确定要执行这个操作么？",function(){
				var userId = getSelectedRow();
	        	 
	            if (userId == null) {
	                return;
	            }
	            
	            if($("#jqGrid").getRowData(userId).status.indexOf("禁用") > 0 && status == 0){
	            	alert("该用户已经被禁用！");
	            	return ;
	            }else if($("#jqGrid").getRowData(userId).status.indexOf("正常") > 0 && status == 1){
	            	alert("该用户已经是正常状态！");
	            	return ;
	            }          
	            
	            $.get(apiURL + "sys/users/openOrClose/" + userId + "/" + status , function (r) {
	                if (r.code == 0) {
	                    alert('操作成功', function (index) {
	                        vm.reload();
	                    });
	                } else {
	                    alert(r.msg);
	                }
	            });
			});        	
        }
	}
});